package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.model.domian.Menu;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.model.dto.ResultObj;
import design.graduation.novelsystem.model.vo.MenuVo;
import design.graduation.novelsystem.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 洪少尉
 * @Date: 2020/4/14 22:54
 * @Description: 菜单管理
 */
@RestController
@RequestMapping("menu")
public class MenuController {
	@Autowired
	private MenuService menuService;
	/**
	 * 管理员登录后加载所有菜单
	 */
	@GetMapping("loadIndexMenu")
	public Object loadIndexMenu(){
		return this.menuService.getIndexMenu();
	}
	/**
	 * 查询菜单和权限
	 * 用于加载所页面表单
	 */
	@GetMapping("loadAllMenuAndPermission")
	@RequiresPermissions("menu:show")
	public Object loadAllMenuAndPermission(MenuVo menuVo){
		return this.menuService.queryAllMenuAndPermission(menuVo);
	}
	/**
	 * 添加
	 */
	@PostMapping("addMenu")
	//@RequiresPermissions("menu:add")
	public ResultObj addMenu(Menu menu){
		if (this.menuService.saveMenu(menu)){
			return ResultObj.ADD_SUCCESS;
		}
		return ResultObj.ADD_ERROR;
	}
	/**
	 * 删除
	 */
	@PostMapping("deleteMenu")
	//@RequiresPermissions("menu:delete")
	public ResultObj deleteMenu(Integer id){
		try {
			this.menuService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		}catch (Exception e){
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	/**
	 * 修改
	 */
	@PostMapping("updateMenu")
	//@RequiresPermissions("menu:update")
	public ResultObj updateMenu(Menu menu){
		try {
			this.menuService.updateById(menu);
			return ResultObj.UPDATE_SUCCESS;
		}catch (Exception e){
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
	/**
	 * 查询菜单和权限最大的排序码
	 * 用于添加
	 */
	@GetMapping("queryMenuMaxSortCode")
	public Object queryMenuMaxSortCode(){
		Integer maxValue=this.menuService.queryMenuMaxSortCode();
		return new DataGridView(maxValue+1);
	}
	/**
	 * 查询菜单
	 * @param menuVo
	 * @return
	 */
	@GetMapping("loadMenu")
	public Object loadMenu(MenuVo menuVo){
		return new DataGridView((long) this.menuService.queryAllMenu().size(),this.menuService.queryAllMenu());
	}
	/**
	 * 根据ID查询当前菜单和权限的子菜单和权限的个数
	 * 用于删除
	 */
	@GetMapping("getMenuChildrenCountById")
	public Object getMenuChildrenCountById(Integer id){
		return new DataGridView(this.menuService.queryMenuChildrenCountById(id));
	}

	/**
	 * 用于修改菜单时，回写菜单数据
	 * @param id 菜单id
	 * @return 菜单数据
	 */
	@GetMapping("getMenuById")
	public Object getMenuById(Integer id){
		return new DataGridView(this.menuService.getById(id));
	}

}
